﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class PreviewApplicationForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindApplication();
            }
        }

        private void BindApplication()
        {
            ApplicationService service = new ApplicationService();
            // 更新ApplicationForm 设置IsSubmit为True
            //service.PreviewApplication(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));

            ApplicationForm application = service.GetApplicationFormDetailByID(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            runningNumberLabel.Text = application.RunningNumber;
            companyLabel.Text = application.User.FullName;
            addressLabel.Text = application.User.UserAddress;
            phoneNumberLabel.Text = application.User.UserPhone;
            faxLabel.Text = application.User.UserFax;
            contactLabel.Text = application.User.ContactPerson;
            emailLabel.Text = application.User.UserEmail;
            cellPhoneLabel.Text = application.User.UserMobilePhone;
            agentLabel.Text = application.ProviderName;
                        
            if (application.AgentID != null)
            {
                UserService userservice = new UserService();
                User agent = userservice.GetUserByID(application.AgentID.Value);
                agentpersonLabel.Text = agent.ContactPerson;
                agentpersonPhoneLabel.Text = agent.UserMobilePhone;
            }

            productGridView.DataSource = application.ApplicationProduct;
            productGridView.DataBind();

            hfApplicationType.Value = application.ApplicationType;
            applicationTypeLbl.Text = GetApplicationType(Request.QueryString["Type"]);
        }

        /// <summary>
        /// 朱经理 2010-06-02 12:43:26
        /// 当客户填写单子 提交后要有MAIL通知到分配号码的人员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            ApplicationService service = new ApplicationService();
            service.SubmitApplication(long.Parse(Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            //UserService userservice = new UserService();
            //List<User> userList = userservice.GetUsersByType((int)RoleType.Service);
            //string body = WebConfigurationManager.AppSettings["SubmitApplication"];
            //foreach (User serviceuser in userList)
            //{
            //    EmailService.SendEmail(serviceuser.UserEmail,"新提交的申请单",string.Format(body,serviceuser.UserName,GetApplicationType(hfApplicationType.Value)));
            //}
            //EmailService.SendEmail("dcn_rma@digitalchina.com", "新提交的申请单", string.Format(body, "", GetApplicationType(hfApplicationType.Value)));
            EmailService.SendEmailToPDYWhenSubitApplicationForm(ContextService.CurrentAccount , GetApplicationType(hfApplicationType.Value));
            ServiceStationService stationService = new ServiceStationService();
            ServiceStation bjStation = stationService.GetHeadServiceStation();
            Response.Write(string.Format("<script language='JavaScript'> alert( '您已经成功提交申请，我们会在2小时内给您授权申请单号，请登陆系统《我的申请表》中查询，并根据系统MAIL反馈的发货地址及时发货，并在外箱上注明RMA#，如果2小时内您未得到信息请联系我们{0}！');location.href= 'MyApplication.aspx' </script> ", bjStation.Phone)); 
        }

        private string GetApplicationType(string p)
        {
            string applicationType = string.Empty;
            switch (p)
            {
                case "R":
                    applicationType = "RMA申请单";
                    break;
                case "D":
                    applicationType = "DOA申请单";
                    break;
                case "S":
                    applicationType = "备件先行申请单";
                    break;
                case "C":
                    applicationType = "库存清理申请单";
                    break;
            }

            return applicationType;
        }

        protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void backLnk_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Type"].Equals("R"))
            {
                Response.Redirect(string.Format("FillRMA.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            }
            else if (Request.QueryString["Type"].Equals("D"))
            {
                Response.Redirect(string.Format("FillDOA.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            }
            else if (Request.QueryString["Type"].Equals("C"))
            {
                Response.Redirect(string.Format("FillSR.aspx?{0}={1}", Constants.QUERYSTR_APPLICATION_ID, Request.QueryString[Constants.QUERYSTR_APPLICATION_ID]));
            }
        }
    }
}
